<?php
//include config alfresco file
	
// include Alfresco PHP API Objects
	require_once "../Alfresco/Service/Repository.php";
	require_once "../Alfresco/Service/Session.php";
	require_once "../Alfresco/Service/SpacesStore.php";
	require_once "../Examples/config.php";
	require_once "../Alfresco/Service/UserDetail.php";
	require_once "../Alfresco/Service/Administration.php";
	require_once "../Alfresco/Service/NamedValues.php";
	//Authenticate the user and create a session
	$repository = new Repository($repositoryUrl);
	$ticket = $repository->authenticate($userName,$password);
	$session = $repository->createSession($ticket);
	$spacesStore = new SpacesStore($session);
	$administration = new Administration($repository,$spacesStore,$session);
	
	//include object for MySQL
	include_once('../configs/configs.php');
	include_once('../connect/DBAccess.php');
	include_once('../model/usermodel.php');
	include_once('../model/phongban.php');
	 $sql = 'set names "utf8"';
        @ mysql_query($sql);
	if(isset($_POST['submit']) && $_POST['submit']=='Thêm')
	{
		$ten = $_POST['firstname'];
		$ho = $_POST['lastname'];
		$gioitinh = $_POST['gender'];
		$ngay = $_POST['ngay'];
		$thang = $_POST['thang'];
		$nam = $_POST['nam'];
		$cmnd = $_POST['cmnd'];
		$address = $_POST['address'];
		$username = $_POST['username'];
		$password = $_POST['password'];
		$cfpassword = $_POST['cfpassword'];
		$email = $_POST['email'];
		 $timestamp = time();
		$file_name = $_FILES['file']['name'] . " (" . $timestamp . ")";	
		$file_tmp = $_FILES['file']['tmp_name'];	
		$file_type = $_FILES['file']['type'];
		$file_size = $_FILES['file']['size'];
		$hoten= $ho. ' '. $ten;
		$ngaysinh = $nam.'-'.$thang .'-'.$ngay;
		echo $ngaysinh;
		$rs = checkUser($username);
		if($hoten!='' && $cmnd!='' && $nam!=''&& $address!='' && $username!='' && $password!=''&& $cfpassword!='' && $email!='' && $rs==false)
		{

			move_uploaded_file($_FILES["file"]["tmp_name"],"../upload/" . $_FILES["file"]["name"]);
			$urlavatar = "../upload".$file_name;
			$rs =insertUser($hoten,$ngaysinh,$cmnd,$email,$address,$username,$password,$gioitinh,$urlavatar);
			// Insert into alfresco
			$NamedValues = new NamedValues($session);
			$NamedValues->cm_firstName = $ten;
			$NamedValues->cm_lastName = $ho;
			$NamedValues->cm_email = $email;
			
			$userDetails = array(	"userName"=>$username,
									"password"=>$password,
									"properties"=>$NamedValues->__toArray());
			
			try{
				$administration->createUser($userDetails);
			}
			catch(Exception $e)
			  {
				echo 'Message: ' .$e->getMessage();
			  }
			if($rs){
			//	 header('location:../view/user/adduser.php?idsuccess=true');
			}
			else  header('location:../view/user/adduser.php?idsuccess=false');
		}
		else {
			print "Vui long nhap du thong tin. <a href='javascript:history.go(-1)'>Quay lai</a>";
			exit;
		}
	}
	if(isset($_POST['submit']) && $_POST['submit']=='Thêm phòng ban')
	{
		$tenpb = $_POST['tenpb'];
		$chuvt = $_POST['chuvt'];
		$rs = insertPB($tenpb,$chuvt);
		
		if($rs)
		{
			header('location:../view/user/addphongban.php?idsuccess=true');
		}
		else  header('location:../view/user/addphongban.php?idsuccess=false');
	}
	//code java
 
   
	
?>